package com.itheima.day20.demo06;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

/**
 * 4. 已知项目下有个a.txt文件，里边存的有5个学员的信息（格式：姓名，语文成绩，数学成绩，英语成绩）
 * 示例：
 * 张三,37,90,100
 * 李四,90,73,81
 * 王五,60,90,76
 * 赵六,89,21,72
 * 田七,100,100,100
 * <p>
 * 需求：
 * 创建输入流对象，读取文本文件中的信息，然后按照总分从高到低的顺序，将排序后的学生信息写入到项目下的b.txt文本文件中。
 * 格式如下：
 * 田七，100,100,100
 * 李四，90,73,81
 * 张三，37,90,100
 * 王五，60,90,76
 * 赵六，89,21,72
 */
public class demo04 {
    public static final String SRC = "./homework/src/com/itheima/day20/demo06/stu2.txt";
    public static final String DEST = "./homework/src/com/itheima/day20/demo06/student2.txt";

    public static void main(String[] args) throws Exception {
        ArrayList<Stu> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(new File(SRC)));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(DEST)));
        String line = null;
        while ((line = br.readLine()) != null) {
            String[] split = line.split(",");
            list.add(new Stu(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3])));
        }
/*        Collections.sort(list,(e1,e2)->{
            int num1=e2.getAllGrade()-e1.getAllGrade();
            return  num1==0?1:num1;
        });*/
        Collections.sort(list);
        for (Stu stu : list) {
            bw.write(stu.toString());
            bw.newLine();
            //System.out.println(stu.toString());
        }
        bw.close();
        br.close();

    }
}

class Stu implements Comparable<Stu> {
    private String name;
    private int math;
    private int chinese;
    private int English;

    public int getAllGrade() {
        return math + chinese + English;
    }

    @Override
    public String toString() {
        return name + "," + math + "," + chinese + "," + English;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getEnglish() {
        return English;
    }

    public void setEnglish(int english) {
        English = english;
    }

    public Stu(String name, int math, int chinese, int english) {
        this.name = name;
        this.math = math;
        this.chinese = chinese;
        English = english;
    }

    public Stu() {
    }


    @Override
    public int compareTo(Stu o) {
        int num1 = o.getAllGrade() - this.getAllGrade();
        return num1 == 0 ? 1 : num1;
    }
}